package com.ttg.web.service.Impl;

import com.ttg.common.enums.AccountDimTypeEnum;
import com.ttg.common.enums.AccountItemTypeEnum;
import com.ttg.common.enums.AcctSubjectTypeEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.model.dto.CMAcctAnalysisDTO;
import com.ttg.model.po.AcctAnalysisPO;
import com.ttg.model.pojo.CostManageAcctComprehensiveAnalysis;
import com.ttg.model.pojo.CostManageAcctDepositAnalysis;
import com.ttg.model.vo.CMAcctComprehensiveAnalysisVO;
import com.ttg.model.vo.CMAcctCostAnalysisVO;
import com.ttg.model.vo.CMAcctDepositAnalysisVO;
import com.ttg.web.dao.CostManageAcctComprehensiveAnalysisDao;
import com.ttg.web.dao.CostManageAcctDepositAnalysisDao;
import com.ttg.web.service.AcctComprehensiveAnalysisService;
import com.ttg.web.service.AcctDepositAnalysisService;
import com.ttg.web.service.CostAnalysisService;
import com.ttg.web.utils.AccountDimTypeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/9/30 15:21
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Slf4j
@Service
public class CostAnalysisServiceImpl implements CostAnalysisService {

    @Autowired
    private CostManageAcctDepositAnalysisDao acctDepositAnalysisDao;
    @Autowired
    private CostManageAcctComprehensiveAnalysisDao acctComprehensiveAnalysisDao;
    @Autowired
    private AcctDepositAnalysisService acctDepositAnalysisService;
    @Autowired
    private AcctComprehensiveAnalysisService acctComprehensiveAnalysisService;


    /**
     * 成本分解清算
     * @param reqDto
     * @date: 2025/10/9 15:54
     * @author: chenyou
     */
    @Override
    public CMAcctCostAnalysisVO acctCostAnalysis(CMAcctAnalysisDTO reqDto) {
        CMAcctCostAnalysisVO vo = new CMAcctCostAnalysisVO();
        //直绑
        reqDto.setAcctDimType(AccountDimTypeEnum.直绑账户.getValue());
        List<CMAcctDepositAnalysisVO> bindDepositList = acctDepositAnalysis(reqDto);
        List<CMAcctComprehensiveAnalysisVO> bindComprehensiveList = acctComprehensiveAnalysis(reqDto);

        //全量
        reqDto.setAcctDimType(AccountDimTypeEnum.全量账户.getValue());
        List<CMAcctDepositAnalysisVO> allDepositList = acctDepositAnalysis(reqDto);
        List<CMAcctComprehensiveAnalysisVO> allComprehensiveList = acctComprehensiveAnalysis(reqDto);
        vo.setBindAcctDepositAnalysisList(bindDepositList);
        vo.setBindAcctComprehensiveAnalysisList(bindComprehensiveList);
        vo.setAllAcctDepositAnalysisList(allDepositList);
        vo.setAllAcctComprehensiveAnalysisList(allComprehensiveList);
        return vo;
    }

    /**
     * 账户存款结构分解清算
     * @param reqDto
     * @date: 2025/10/9 9:05
     * @author: chenyou
     */
    public List<CMAcctDepositAnalysisVO> acctDepositAnalysis(CMAcctAnalysisDTO reqDto) {

        //清算组
        AcctAnalysisPO po = new AcctAnalysisPO();
        po.setIntervalType(reqDto.getIntervalType());
        po.setAcctDimType(reqDto.getAcctDimType());
        po.setYear(reqDto.getSettleYear());
        po.setMonth(reqDto.getSettleMonth());
        List<CostManageAcctDepositAnalysis> settList = acctDepositAnalysisDao.getByParam(po);
        if(CollectionUtils.isEmpty(settList) || settList.size()!=2){
            throw new BaseException("清算年度/月度数据不全");
        }

        //对照组
        po.setYear(reqDto.getComparisonYear());
        po.setMonth(reqDto.getComparisonMonth());
        List<CostManageAcctDepositAnalysis> comparisonList = acctDepositAnalysisDao.getByParam(po);
        if(CollectionUtils.isEmpty(comparisonList) || comparisonList.size()!=2){
            throw new BaseException("对照年度/月度数据不全");
        }

        //对照轧差
        CMAcctDepositAnalysisVO a1 = acctDepositAnalysisService.depositComparisonNetting(reqDto,settList,comparisonList, AcctSubjectTypeEnum.个人户);
        CMAcctDepositAnalysisVO a2 = acctDepositAnalysisService.depositComparisonNetting(reqDto,settList,comparisonList, AcctSubjectTypeEnum.对公户);
        //合计
        CMAcctDepositAnalysisVO total = acctDepositAnalysisService.total(reqDto,a1,a2);
        //本地线程保存计算结果,后续计算需要使用
        AccountDimTypeEnum dimTypeEnum = AccountDimTypeEnum.getByVal(reqDto.getAcctDimType());
        AccountDimTypeUtil.putResult(dimTypeEnum,"Js",total.getNettingAvgAmt());
        AccountDimTypeUtil.putResult(dimTypeEnum,"Ks",total.getNettingWeightRate());
        AccountDimTypeUtil.putResult(dimTypeEnum,"Ls",total.getNettingPayInterest());
        return Arrays.asList(a1,a2,total);
    }

    /**
     * 账户综合结构分解清算
     * @param reqDto
     * @date: 2025/10/9 9:05
     * @author: chenyou
     */
    public List<CMAcctComprehensiveAnalysisVO> acctComprehensiveAnalysis(CMAcctAnalysisDTO reqDto) {
        //清算组
        AcctAnalysisPO po = new AcctAnalysisPO();
        po.setIntervalType(reqDto.getIntervalType());
        po.setAcctDimType(reqDto.getAcctDimType());
        po.setYear(reqDto.getSettleYear());
        po.setMonth(reqDto.getSettleMonth());
        List<CostManageAcctComprehensiveAnalysis> settList = acctComprehensiveAnalysisDao.getByParam(po);
        if(CollectionUtils.isEmpty(settList) || settList.size()!=5){
            throw new BaseException("清算年度/月度数据不全");
        }
        //对照组
        po.setYear(reqDto.getComparisonYear());
        po.setMonth(reqDto.getComparisonMonth());
        List<CostManageAcctComprehensiveAnalysis> comparisonList = acctComprehensiveAnalysisDao.getByParam(po);
        if(CollectionUtils.isEmpty(comparisonList) || comparisonList.size()!=5){
            throw new BaseException("对照年度/月度数据不全");
        }
        //对照轧差
        CMAcctComprehensiveAnalysisVO i1 = acctComprehensiveAnalysisService.comprehensiveComparisonNetting(reqDto,settList,comparisonList, AccountItemTypeEnum.综合付息);
        CMAcctComprehensiveAnalysisVO i2 = acctComprehensiveAnalysisService.comprehensiveComparisonNetting(reqDto,settList,comparisonList, AccountItemTypeEnum.结算手续费);
        CMAcctComprehensiveAnalysisVO i3 = acctComprehensiveAnalysisService.comprehensiveComparisonNetting(reqDto,settList,comparisonList, AccountItemTypeEnum.云音响费用);
        CMAcctComprehensiveAnalysisVO i4 = acctComprehensiveAnalysisService.comprehensiveComparisonNetting(reqDto,settList,comparisonList, AccountItemTypeEnum.绩效和计价);
        CMAcctComprehensiveAnalysisVO i5 = acctComprehensiveAnalysisService.comprehensiveComparisonNetting(reqDto,settList,comparisonList, AccountItemTypeEnum.涉贷利息差);
        return Arrays.asList(i1,i2,i3,i4,i5);
    }


}
